{$CLEO}
{$I asmdef.txt}

const
    LIGHT_STATES_OFFSET = 0x9E4 // CHeli start - unused for automobiles
end

0AC6: 0@ = label @__asma offset
0AC6: 1@ = label @jmp_backa offset
0AB1: call_scm_func @asmdef 4 asm_inj 0@ jump_from 0x6E18DA then_jump 0x6E18EB offset_jump_in_asm 1@
0AC6: 0@ = label @__asmb offset
0AC6: 1@ = label @jmp_backb offset
0AB1: call_scm_func @asmdef 4 asm_inj 0@ jump_from 0x6E1A2D then_jump 0x6E1A32 offset_jump_in_asm 1@
0AC6: 0@ = label @getTexture offset
0AC6: 1@ = label @jmp offset
0AB1: call_scm_func @asmdef 4 asm_inj 0@ jump_from 0x4C8311 then_jump 0x4C8319 offset_jump_in_asm 1@
0AC6: 0@ = label @storeModel offset
0AC6: 1@ = label @jmp2 offset
0AB1: call_scm_func @asmdef 4 asm_inj 0@ jump_from 0x6D6622 then_jump 0x6D6627 offset_jump_in_asm 1@
0AC6: 0@ = label @getTexture2 offset
0AC6: 1@ = label @jmp3 offset
0AB1: call_scm_func @asmdef 4 asm_inj 0@ jump_from 0x4C826B then_jump 0x4C8337 offset_jump_in_asm 1@
//getTexture
0AC6: 0@ = label @getTexture offset
0AC6: 1@ = label @renderedModel offset
0@ += 39
0A8C: 0@ 4 1@ 0
0AC6: 0@ = label @new offset
0AC6: 2@ = label @lights_on offset
0A8C: 0@ 4 2@ 0
//getTexture2
0AC6: 0@ = label @getTexture2 offset
0AC6: 1@ = label @renderedModel offset
0@ += 7
0A8C: 0@ 4 1@ 0
0AC6: 0@ = label @new2 offset
0AC6: 2@ = label @lights offset
0A8C: 0@ 4 2@ 0
//storeModel
0AC6: 0@ = label @storeModel offset
0@ += 10
0A8C: 0@ 4 1@ 0
// patching _storeRenderLightParamsForVehicle 0x4C8C90
0AC6: 0@ = label @storelight offset
0AC6: 1@ = label @jmp4 offset
0AC6: 2@ = label @lightstates offset
0A8C: 0x4C82D8 4 2@ 1
0AB1: call_scm_func @asmdef 4 asm_inj 0@ jump_from 0x4C8C94 then_jump 0x4C8CD8 offset_jump_in_asm 1@
0AC6: 0@ = label @sl_1 offset
inc(2@)
0A8C: write_memory 0@ size 4 value 2@ virtual_protect 0
dec(2@)
0AC6: 0@ = label @sl_2 offset
0A8C: write_memory 0@ size 4 value 2@ virtual_protect 0
inc(2@, 3)
0AC6: 0@ = label @sl_3 offset
0A8C: write_memory 0@ size 4 value 2@ virtual_protect 0
dec(2@)
0AC6: 0@ = label @sl_4 offset
0A8C: write_memory 0@ size 4 value 2@ virtual_protect 0
0@ += 6
2@ += 2
4@ = LIGHT_STATES_OFFSET
4@ += 4
for 3@ = 0 to 11
    0A8C: write_memory 0@ size 4 value 4@ virtual_protect 0
    inc(4@)
    0@ += 6
    0A8C: write_memory 0@ size 4 value 2@ virtual_protect 0
    inc(2@)
    0@ += 6
end
// testing new materials
0AC6: 0@ = label @testmaterial offset
0AC6: 1@ = label @jmp5 offset
0AB1: call_scm_func @asmdef 4 asm_inj 0@ jump_from 0x4C82A4 then_jump 0x4C82B1 offset_jump_in_asm 1@
// test for damage texture
0AC6: 0@ = label @dam offset
0AC6: 1@ = label @jmp6 offset
0AB1: call_scm_func @asmdef 4 asm_inj 0@ jump_from 0x4C82DC then_jump 0x4C83AA offset_jump_in_asm 1@
0AC6: 1@ = label @renderedModel offset
0@ += 34
0A8C: 0@ 4 1@ 0
0AC6: 0@ = label @new6 offset
0AC6: 2@ = label @lights_dam offset
0A8C: 0@ 4 2@ 0

// Dirt level fixing
0A8C: 0x5D5DD5 5 0x90 1
0A8C: 0x5D5D83 5 0x90 1

// create new RestoreMaterialData buffer
0AC8: 10@ = allocate_memory_size 200000
0A8C: 0x4C8444 4 10@ 1
0A8C: 0x4C8462 4 10@ 1
0A8C: 0x4C8469 4 10@ 1

0AC6: 0@ = label @__asm offset
0AC6: 1@ = label @__jmp offset
0AB1: call_scm_func @asmdef 4 asm_inj 0@ jump_from 0x4C8337 then_jump 0x4C833D offset_jump_in_asm 1@
0AC6: 0@ = label @storedirt offset
0AC6: 1@ = label @__jmp2 offset
0AB1: call_scm_func @asmdef 4 asm_inj 0@ jump_from 0x6D661C then_jump 0x6D6622 offset_jump_in_asm 1@
0@ += 17
0AC6: 1@ = label @dirtLevel offset
0A8C: 0@ 4 1@ 0
0AC6: 2@ = label @helper_01 offset
0A8C: 2@ 4 1@ 0

{$I ImVehLM_loop.txt} 

:dam
hex
80 FA 01             // cmp     dl, 1
75 07                // jne
BA E4 82 4C 00       // mov     edx, 0x4C82E4
FF E2                // jmp     edx
80 FA 02             // cmp     dl, 2
75 66 //00 00 00    // jne     +66

8B 19                // mov     ebx, [ecx]
89 03                // mov     [ebx], eax
83 C3 04             // add     ebx, 4
8B 30                // mov     esi, [eax]
89 33                // mov     [ebx], esi
83 01 08             // add     dword ptr [ecx], 8

51                   // push    ecx
50                   // push    eax 
A1 00 00 00 00       // mov     eax, [RenderedModel]
8B 04 85 C8 B0 A9 00 // mov     eax, _modelPtrs[eax*4]
0F BF 48 0A          // movsx   ecx, word ptr [eax+0Ah]
8B 15 0C 80 C8 00    // mov     edx, _TexDictionaryPool
8B 72 04             // mov     esi, [edx+4]
80 3C 31 00          // cmp     byte ptr [ecx+esi], 0
79 04                // jns     short +4
33 C9                // xor     ecx, ecx
EB 08                // jmp     short +8
8B 12                // mov     edx, [edx]
8D 0C 49             // lea     ecx, [ecx+ecx*2]
8D 0C 8A             // lea     ecx, [edx+ecx*4]
8B 31                // mov     esi, [ecx]
85 F6                // test    esi, esi
74 23                // jz      short +0x23
68 
end

:new6
hex
00 00 00 00          // push    offset "lights_dam"
B8 00 CF 53 00       // mov     eax, offset _crc32FromString 0x53CF00 // 70   
FF D0                // call    eax
50                   // push    eax
56                   // push    esi
B8 50 4E 73 00       // mov     eax, offset sub_734E50
FF D0                // call    eax
83 C4 0C             // add     esp, 0xC
85 C0                // test    eax, eax
74 07                // jz      short +7
8B C8                // mov     ecx, eax                                            
58                   // pop     eax
89 08                // mov     [eax], ecx
EB 01                // jmp     short 1
58                   // pop     eax
59                   // pop     ecx
end

:jmp6
hex
    E9 00 00 00 00   // jmp
end

:storelight
hex
    8B F8             // mov     edi, eax
    8A 97 84 05 00 00 // mov     dl, byte ptr [edi+CVehicle.dwRenderLights]
    80 E2 01          // and     dl, 1
    6A 01             // push    1
    8D 8F A0 05 00 00 // lea     ecx, [edi+0x5A0]
    B8 30 21 6C 00    // mov     eax, 0x6C2130
    FF D0             // call    eax
    85 C0             // test    eax, eax
    74 02             // jz     short +2
    B2 02             // mov     dl, 2
    88 15 
end
    
:sl_1
hex
    00 00 00 00       // mov     @lightstates[1], dl
    8A 97 84 05 00 00 // mov     dl, byte ptr [edi+CVehicle.dwRenderLights]
    D0 EA             // shr     dl, 1
    80 E2 01          // and     dl, 1
    6A 00             // push    0
    8D 8F A0 05 00 00 // lea     ecx, [edi+0x5A0]
    B8 30 21 6C 00    // mov     eax, 0x6C2130
    FF D0             // call    eax
    85 C0             // test    eax, eax
    74 02             // jz     short +2
    B2 02             // mov     dl, 2
    88 15 
end

:sl_2
hex    
    00 00 00 00       // mov     @lightstates[0], dl
    8A 97 84 05 00 00 // mov     dl, byte ptr [edi+CVehicle.dwRenderLights]
    C0 EA 02          // shr     dl, 2
    80 E2 01          // and     dl, 1
    6A 02             // push    3
    8D 8F A0 05 00 00 // lea     ecx, [edi+0x5A0]
    B8 30 21 6C 00    // mov     eax, 0x6C2130
    FF D0             // call    eax
    85 C0             // test    eax, eax
    74 02             // jz     short +2
    B2 02             // mov     dl, 2
    88 15 
end    

:sl_3
hex    
    00 00 00 00       // mov     @lightstates[3], dl
    8A 97 84 05 00 00 // mov     dl, byte ptr [edi+CVehicle.dwRenderLights]
    C0 EA 03          // shr     dl, 3
    80 E2 01          // and     dl, 1
    6A 03             // push    2
    8D 8F A0 05 00 00 // lea     ecx, [edi+0x5A0]
    B8 30 21 6C 00    // mov     eax, 0x6C2130
    FF D0             // call    eax
    85 C0             // test    eax, eax
    74 02             // jz     short +2
    B2 02             // mov     dl, 2
    88 15 
end

:sl_4
hex    
    00 00 00 00       // mov     @lightstates[2], dl
    8A 97 00 00 00 00 // mov     dl, byte ptr [eax+CVehicle.bRenderLight[4]]
    88 15 00 00 00 00 // mov     @lightstates[4], dl
    8A 97 00 00 00 00 // mov     dl, byte ptr [eax+CVehicle.bRenderLight[5]]
    88 15 00 00 00 00 // mov     @lightstates[5], dl
    8A 97 00 00 00 00 // mov     dl, byte ptr [eax+CVehicle.bRenderLight[6]]
    88 15 00 00 00 00 // mov     @lightstates[6], dl
    8A 97 00 00 00 00 // mov     dl, byte ptr [eax+CVehicle.bRenderLight[7]]
    88 15 00 00 00 00 // mov     @lightstates[7], dl
    8A 97 00 00 00 00 // mov     dl, byte ptr [eax+CVehicle.bRenderLight[8]]
    88 15 00 00 00 00 // mov     @lightstates[8], dl
    8A 97 00 00 00 00 // mov     dl, byte ptr [eax+CVehicle.bRenderLight[9]]
    88 15 00 00 00 00 // mov     @lightstates[9], dl
    8A 97 00 00 00 00 // mov     dl, byte ptr [eax+CVehicle.bRenderLight[10]]
    88 15 00 00 00 00 // mov     @lightstates[10], dl
    8A 97 00 00 00 00 // mov     dl, byte ptr [eax+CVehicle.bRenderLight[11]]
    88 15 00 00 00 00 // mov     @lightstates[11], dl
    8A 97 00 00 00 00 // mov     dl, byte ptr [eax+CVehicle.bRenderLight[12]]
    88 15 00 00 00 00 // mov     @lightstates[12], dl
    8A 97 00 00 00 00 // mov     dl, byte ptr [eax+CVehicle.bRenderLight[13]]
    88 15 00 00 00 00 // mov     @lightstates[13], dl
    8A 97 00 00 00 00 // mov     dl, byte ptr [eax+CVehicle.bRenderLight[14]]
    88 15 00 00 00 00 // mov     @lightstates[14], dl
    8A 97 00 00 00 00 // mov     dl, byte ptr [eax+CVehicle.bRenderLight[15]]
    88 15 00 00 00 00 // mov     @lightstates[15], dl
end

:jmp4
hex
    E9 00 00 00 00   // jmp
end

{
RGB

F_L           FOG LEFT
FF AE 00
R_L           REV LEFT
FF AD 00

F_R           FOG RIGHT
00 FF C7
R_R           REV LIGHT
00 FF C6

B_L           BREAK LEFT
B8 FF 00
T_LF          TURN LEFT FRONT
B7 FF 00
T_LM          TURN LEFT MIDDLE
B6 FF 00
T_LR          TURN LEFT REAR
B5 FF 00

B_R           BREAK RIGHT
FF 3B 00
T_RF          TURN RIGHT FRONT
FF 3A 00
T_RM          TURN RIGHT MIDDLE
FF 39 00
T_RR          TURN RIGHT REAR
FF 38 00

}

:testmaterial
hex
81 FE FF 3C 00 00 // cmp     esi, 3CFFh
75 0A             // jnz     short +7
BF 03 00 00 00    // mov     edi, 3
E9 BE 00 00 00    // jmp     

81 FE B7 FF 00 00 // cmp     esi, 
75 0A             // jnz     short +7
BF 0A 00 00 00    // mov     edi, 10
E9 AC 00 00 00    // jmp     

81 FE B6 FF 00 00 // cmp     esi, 
75 0A             // jnz     short +7
BF 0B 00 00 00    // mov     edi, 11
E9 9A 00 00 00    // jmp    

81 FE B5 FF 00 00 // cmp     esi, 
75 0A             // jnz     short +7
BF 0C 00 00 00    // mov     edi, 12
E9 88 00 00 00    // jmp     

81 FE FF 3A 00 00 // cmp     esi, 
75 0A             // jnz     short +7
BF 0D 00 00 00    // mov     edi, 13
E9 76 00 00 00    // jmp     

81 FE FF 39 00 00 // cmp     esi, 
75 07             // jnz     short +7
BF 0E 00 00 00    // mov     edi, 14
EB 67             // jmp     short    

81 FE FF 38 00 00 // cmp     esi, 
75 07             // jnz     short +7
BF 0F 00 00 00    // mov     edi, 15
EB 58             // jmp     short

81 FE B8 FF 00 00 // cmp     esi, 
75 07             // jnz     short +7
BF 04 00 00 00    // mov     edi, 4
EB 49             // jmp     short

81 FE FF 3B 00 00 // cmp     esi, 
75 07             // jnz     short +7
BF 05 00 00 00    // mov     edi, 5
EB 3A             // jmp     short    

81 FE FF AE 00 00 // cmp     esi, 
75 07             // jnz     short +7
BF 08 00 00 00    // mov     edi, 8
EB 2B             // jmp     short

81 FE 00 FF C7 00 // cmp     esi, 
75 07             // jnz     short +7
BF 09 00 00 00    // mov     edi, 9
EB 1C             // jmp     short 

81 FE FF AD 00 00 // cmp     esi, 
75 07             // jnz     short +7
BF 06 00 00 00    // mov     edi, 6
EB 0D             // jmp     short

81 FE 00 FF C6 00 // cmp     esi, 
75 05             // jnz     short +5
BF 07 00 00 00    // mov     edi, 7
end

:jmp5
hex
    E9 00 00 00 00   // jmp
end

:getTexture
hex
8B 19                // mov     ebx, [ecx]  // fixing Rockstar's bug, that after switching lights materials loss specular and diffuse color
83 C2 04             // add     edx, 4
89 13                // mov     [ebx], edx
83 C3 04             // add     ebx, 4
8B 32                // mov     esi, [edx]
89 33                // mov     [ebx], esi
83 C3 04             // add     ebx, 4
83 C2 04             // add     edx, 4
89 13                // mov     [ebx], edx
83 C3 04             // add     ebx, 4
8B 32                // mov     esi, [edx]
89 33                // mov     [ebx], esi
83 EA 08             // sub     edx, 8
83 01 10             // add     dword ptr [ecx], 0x10 // end of fixing
50                   // push    eax
52                   // push    edx
56                   // push    esi   
A1 00 00 00 00       // mov     eax, [RenderedModel]
8B 04 85 C8 B0 A9 00 // mov     eax, _modelPtrs[eax*4]
0F BF 48 0A          // movsx   ecx, word ptr [eax+0Ah]
8B 15 0C 80 C8 00    // mov     edx, _TexDictionaryPool
8B 72 04             // mov     esi, [edx+4]
80 3C 31 00          // cmp     byte ptr [ecx+esi], 0
79 04                // jns     short +4
33 C9                // xor     ecx, ecx
EB 08                // jmp     short +8
8B 12                // mov     edx, [edx]
8D 0C 49             // lea     ecx, [ecx+ecx*2]
8D 0C 8A             // lea     ecx, [edx+ecx*4]
8B 31                // mov     esi, [ecx]
85 F6                // test    esi, esi
74 20                // jz      short +0x20
68 
end

:new
hex
00 00 00 00          // push    offset "lights"
B8 00 CF 53 00       // mov     eax, offset _crc32FromString 0x53CF00 // 70   
FF D0                // call    eax
50                   // push    eax
56                   // push    esi
B8 50 4E 73 00       // mov     eax, offset sub_734E50
FF D0                // call    eax
83 C4 0C             // add     esp, 0x10
85 C0                // test    eax, eax
74 04                // jz      short
8B C8                // mov     ecx, eax                                            
EB 06                // jmp     short +6
8B 0D 90 E6 B4 00    // ecx,    texVehiclelightson128
5E                   // pop     esi
5A                   // pop     edx
58                   // pop     eex
89 08                // mov     [eax], ecx
end

:jmp
hex
    E9 00 00 00 00   // jmp     0x4C8319
end

:storeModel
hex
A3 7C E4 B4 00        // mov     dword_B4E47C, eax
0F BF 46 22           // movsx   eax, [esi+22h]
A3 00 00 00 00        // mov     dword ptr [RenderedModel], eax
end

:jmp2
hex
E9 00 00 00 00       // jmp
end

:getTexture2
hex
53                   // push    ebx
51                   // push    ecx
57                   // push    edi
50                   // push    eax
52                   // push    edx
56                   // push    esi   
A1 00 00 00 00       // mov     eax, [RenderedModel]
8B 04 85 C8 B0 A9 00 // mov     eax, _modelPtrs[eax*4]
0F BF 48 0A          // movsx   ecx, word ptr [eax+0Ah]
8B 15 0C 80 C8 00    // mov     edx, _TexDictionaryPool
8B 72 04             // mov     esi, [edx+4]
80 3C 31 00          // cmp     byte ptr [ecx+esi], 0
79 04                // jns     short +4
33 C9                // xor     ecx, ecx
EB 08                // jmp     short +8
8B 12                // mov     edx, [edx]
8D 0C 49             // lea     ecx, [ecx+ecx*2]
8D 0C 8A             // lea     ecx, [edx+ecx*4]
8B 31                // mov     esi, [ecx]
85 F6                // test    esi, esi
74 20                // jz      short +0x20
68 
end

:new2
hex
00 00 00 00          // push    offset "lights"
B8 00 CF 53 00       // mov     eax, offset _crc32FromString 0x53CF00 // 70   
FF D0                // call    eax
50                   // push    eax
56                   // push    esi
B8 50 4E 73 00       // mov     eax, offset sub_734E50
FF D0                // call    eax
83 C4 0C             // add     esp, 0x10
85 C0                // test    eax, eax
74 04                // jz      short
8B C8                // mov     ecx, eax                                            
EB 06                // jmp     short +6
8B 0D 8C E6 B4 00    // ecx,    texVehiclelights128
5E                   // pop     esi
5A                   // pop     edx
58                   // pop     eex
5F                   // pop     edi
3B F9                // cmp     edi, ecx
75 09                // jnz     short +9
59                   // pop     ecx
5B                   // pop     ebx
BF 77 82 4C 00       // mov     edi, 0x4C8277
FF E7                // jmp     edi
8B 0D 8C E6 B4 00    // mov     ecx, texVehiclelights128
3B F9                // cmp     edi, ecx
75 09                // jnz     short +9
59                   // pop     ecx
5B                   // pop     ebx
BF 77 82 4C 00       // mov     edi, 0x4C8277
FF E7                // jmp     edi
59                   // pop     ecx
5B                   // pop     ebx
end

:jmp3
hex
E9 00 00 00 00       // jmp
end

:__asm
hex
8B 38             // mov     edi, [eax]
81 FF 00 00 00 00 // cmp     edi, 0
76 3B             // jna     break
83 C7 17          // add     edi, 17
80 3F 67          // cmp     byte ptr[edi], 67 ; g
75 33             // jne     break
47                // inc     edi
80 3F 72          // cmp     byte ptr[edi], 72 ; r
75 2D             // jne     break 
47                // inc     edi
80 3F 75          // cmp     byte ptr[edi], 75 ; u
75 27             // jne     39
// code for mat
//
// store old data
8B 39             // mov     edi, [ecx]
89 07             // mov     [edi], eax
8B 19             // mov     ebx, [ecx]
83 C3 04          // add     ebx, 4
89 19             // mov     [ecx], ebx
8B FB             // mov     edi, ebx
8B 18             // mov     ebx, [eax]
89 1F             // mov     [edi], ebx
83 01 04          // add     dword ptr [ecx], 4

// get vehicle dirt level
50                // push    eax
A1
end 

:helper_01
hex
00 00 00 00       // mov     eax, [dirtLevel]
// get texture
6B C0 04          // imul eax, 4
05 D0 2B C0 00    // add eax, C02BD0h
8B 38             // mov edi, [eax]
58                // pop eax
// set texture
//57              // push edi
//50              // push eax
//BF C0 DB 74 00  // mov edi, 74DBC0h
//FF D7           //call edi
//83 C4 08        // add esp, 8
89 38             // mov [eax], edi
// continue
81 FE 3C FF 00 00
end

:__jmp
hex
E9 00 00 00 00
end

:storedirt
hex
D9 86 B0 04 00 00 // fld     [esi+CVehicle.field_4B0]
B8 40 1B 82 00    // mov     eax, __ftol2
FF D0             // call    eax
83 E0 0F          // and     eax, 0Fh
A3 00 00 00 00    // mov     dword ptr [dirtLevel], eax
8B 86 9C 05 00 00 // mov     eax, [esi+59Ch]
end

:__jmp2
hex
E9 00 00 00 00    // jmp     6D6622h
end

//from tailight script
:__asma
hex
    D8 1D 50 8B 85 00            // fcomp   ds:fZero
    33 C0                        // xor     eax, eax
    DF E0                        // fnstsw  ax
    66 8B D8                     // mov     bx, ax
end

:jmp_backa
hex
    E9 00 00 00 00               // jmp     0
end

:__asmb
hex
    F6 C7 41                     // test    bh, 41
    75 07                        // jnz     short 7
    B8 80 C5 6F 00               // mov     eax, 6FC580
    FF D0                        // call    eax
end

:jmp_backb
hex
    E9 00 00 00 00
end

:renderedModel
hex
    00000000 // ModelID
end

:dirtLevel
hex
00000000
end

:lightstates
hex
    00 // LF
    00 // RF
    00 // LR
    00 // RR
    00 // breaklight L
    00 // breaklight R
    00 // reverselight L
    00 // reverselight R
    00 // foglight L
    00 // foglight R
    00 // turnlight LF
    00 // turnlight LM
    00 // turnlight LR
    00 // turnlight RF
    00 // turnlight RM
    00 // turnlight RR
end

:lights
hex
    "VEHICLELIGHTS" 00
end

:lights_on
hex
    "VEHICLELIGHTS_ON" 00
end

:lights_dam
hex
    "VEHICLELIGHTS_DAM" 00
end